import make_packet
import time_master_send
from tkinter import *
from scapy.all import *

import warnings
warnings.filterwarnings('ignore')
interface = "Intel(R) Wireless-AC 9560 160MHz"

def first_packet(src, dst):
    tf = src.split(".")
    df = dst.split(".")
    src_ip = tf[0] + tf[1] + tf[2] + tf[3]
    dst_ip = df[0] + df[1] + df[2] + df[3]
    id_value = (int(src_ip) & int(dst_ip)) % 65535
    data = IP(src=src, dst=dst, id=id_value) / ICMP(type=8)
    sr(data, iface=interface)  # sr函数，发送方会主动检测是否会有返回ICMP包
    print('隐蔽信道构建成功')
    return id_value




def helloCallBack1():


    message1 = message.get()
    src1 = src.get()
    dst1 = dst.get()
    id = first_packet(src1, dst1)
    password=psd.get()
    num, pkt_list, pkt_end ,time_data=make_packet.make_packets(message=message1,key=password, src=src1, dst=dst1,id=id)
    print(num)
    time_master_send.send_pkt1(num, pkt_list, pkt_end,time_data)


def helloCallBack2():


    message1 = message.get()
    src1 = src.get()
    dst1 = dst.get()
    id = first_packet(src1, dst1)
    password=psd.get()
    num, pkt_list, pkt_end ,time_data=make_packet.make_packets(message=message1,key=password, src=src1, dst=dst1,id=id)
    print(num)
    time_master_send.send_pkt2(num, pkt_list, pkt_end,time_data)


if __name__ == '__main__':


    master = Tk()
    master.title('发送方')
    frame1 = Frame(master)
    frame1.pack(fill=X)
    label1 = Label(frame1, text='输入隐藏的信息：')
    label1.grid(row=1, column=0)
    # 输入框 (Entry)
    message = StringVar()
    entryname1 = Entry(frame1, textvariable=message)
    entryname1.grid(row=1, column=1)
    label2 = Label(frame1, text='输入源IP：')
    label2.grid(row=2, column=0)
    # 输入框 (Entry)
    src = StringVar()
    entryname2 = Entry(frame1, textvariable=src)
    entryname2.grid(row=2, column=1)
    label2 = Label(frame1, text='输入目的IP：')
    label2.grid(row=3, column=0)
    # 输入框 (Entry)
    dst = StringVar()
    entryname3 = Entry(frame1, textvariable=dst)
    entryname3.grid(row=3, column=1)
    label3 = Label(frame1, text='输入密钥：')
    label3.grid(row=4, column=0)
    # 输入框 (Entry)
    psd = StringVar()
    entryname4 = Entry(frame1, textvariable=psd)
    entryname4.grid(row=4, column=1)
    B1 = Button(master, text="顺序发送", command=helloCallBack1)
    B1.pack()
    B2 = Button(master, text="乱序发送", command=helloCallBack2)
    B2.pack()
    master.mainloop()